<?php

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$merchid=$this->checklogin();
if($_RQ['op']=='main'){
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'post'=>'set',
		'desc'=>'按月导出销售统计',
	);
	$htmls['edit']=array(
		array('name'=>'year','title'=>'选择年份','type'=>'select','options'=>'year'),
		array('name'=>'month','title'=>'选择月份','type'=>'select','options'=>'month'),
	);
	$year=intval(date('Y',time()));
	$return['year']=array('0'=>array('id'=>$year-9,'title'=>$year-9),'1'=>array('id'=>$year-8,'title'=>$year-8),'2'=>array('id'=>$year-7,'title'=>$year-7),'3'=>array('id'=>$year-6,'title'=>$year-6),'4'=>array('id'=>$year-5,'title'=>$year-5),'5'=>array('id'=>$year-4,'title'=>$year-4),'6'=>array('id'=>$year-3,'title'=>$year-3),'7'=>array('id'=>$year-2,'title'=>$year-2),'8'=>array('id'=>$year-1,'title'=>$year-1),'9'=>array('id'=>$year,'title'=>$year));
	$return['month']=array('0'=>array('id'=>1,'title'=>1),'1'=>array('id'=>2,'title'=>2),'2'=>array('id'=>3,'title'=>3),'3'=>array('id'=>4,'title'=>4),'4'=>array('id'=>5,'title'=>5),'5'=>array('id'=>6,'title'=>6),'6'=>array('id'=>7,'title'=>7),'7'=>array('id'=>8,'title'=>8),'8'=>array('id'=>9,'title'=>9),'9'=>array('id'=>10,'title'=>10),'10'=>array('id'=>11,'title'=>11),'11'=>array('id'=>12,'title'=>12));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	if(!$post['year']){
		exi('请选择年份！','error');
	}
	if(!$post['month']){
		exi('请选择月份！','error');
	}
	$url=download_url('jy_merch/manage.statistics.dao/export1/year='.$post['year'].'&month='.$post['month']);
	exi('点确定开始下载','','',$url);
}
if($_RQ['op']=='export1'){
	$return['params']=array();
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);
	$lasttime=strtotime($_RQ['year'].'-'.$_RQ['month'].'-01');
	$lasttime=$_RQ['lasttime']?:strtotime('-1 month');
	$year =$_RQ['year'];
	$month =$_RQ['month'];
	$header=array(
		'goods'=>'菜品',
	);
	$newslist=array();
	$onenewslist=array(
		'goods'=>'',
	);
	$onenewslist2=array(
		'goods'=>'日期',
	);
	$fields=array('id','merchid','uniacid','paixu','title','pcate','ccate','price','unit');
	$goodslist=pdo_getall('jy_weishop_goods',$params,$fields);
	foreach($goodslist as $goods){
		$header['title'.$goods['id']]=$goods['title'];
		$header['titlet'.$goods['id']]='';
		$onenewslist['title'.$goods['id']]=$goods['price'].'/份';
		$onenewslist['titlet'.$goods['id']]='';
		$onenewslist2['title'.$goods['id']]='数量';
		$onenewslist2['titlet'.$goods['id']]='金额';
	}
	$header['heji']='合计';
	$header['zhekou']='折扣';
	$header['pay']='实收';
	$header['sao']='扫码';
	$header['xianjin']='现金';
	$onenewslist['heji']='金额';
	$newslist[]=$onenewslist;
	$newslist[]=$onenewslist2;
	$day = date("t",strtotime($year.'-'.$month));
	$i = 1;
	
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1);
	while ($i <= $day){
		$daystart=strtotime($year.'-'.$month.'-'.$i);
		$dayend=$daystart+24*3600;
		$params['createtime between']=array($daystart,$dayend);
		$daydata=array();
		$daydata['goods']=$i;
		$heji=0;
		$zhekou=0;
		$pay=0;
		$sao=0;
		$xianjin=0;
		foreach($goodslist as $goods){
			$params['goodsid']=$goods['id'];
			$saletotal=pdo_getsum('jy_weishop_order_goods',$params,'total');
			$saletotal_price=pdo_getsum('jy_weishop_order_goods',$params,'total_price');
			$daydata['title'.$goods['id']]=$saletotal;
			$daydata['titlet'.$goods['id']]=$saletotal_price;
			$heji +=$saletotal*$goods['price'];
			$pay +=$saletotal_price;
		}
		$daydata['heji']=$heji;
		$daydata['zhekou']=$heji-$pay;
		$daydata['pay']=$pay;
		$newslist[]=$daydata;
		++$i;
	}
	$onenewslist=array();
	$onenewslist['goods']='合计';
	$heji=0;
	$zhekou=0;
	$pay=0;
	$sao=0;
	$xianjin=0;
	$yuestart = mktime(0,0,0,$month,1,$year);
	$yueend = mktime(0,0,0,intval($month)+1,1,$year);
	$params=array('createtime between'=>array($yuestart,$yueend),'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1);
	foreach($goodslist as $goods){
		$params['goodsid']=$goods['id'];
		$saletotal=pdo_getsum('jy_weishop_order_goods',$params,'total');
		$saletotal_price=pdo_getsum('jy_weishop_order_goods',$params,'total_price');
		$onenewslist['title'.$goods['id']]=$saletotal;
		$onenewslist['titlet'.$goods['id']]=$saletotal_price;
		$heji +=$saletotal*$goods['price'];
		$pay +=$saletotal_price;
	}
	$onenewslist['heji']=$heji;
	$onenewslist['zhekou']=$heji-$pay;
	$onenewslist['pay']=$pay;
	$newslist[]=$onenewslist;
	model('core/tools')->export2excel($header,$newslist,'['.$year . '-' . $month . ']销售表');
}